---
title: "Main Results (IV): Other Specifications (School)"
author: "Andrei Munteanu"
date: "15/09/2021"
output:
  html_document:
    fig_width: 8 
    fig_height: 8 
  # html_document:
    # toc: true 
    # toc_float: true
---
<style type="text/css">
.main-container {
  max-width: 1800px;
  margin-left: auto;
  margin-right: auto;
}
</style>

```{r options, include=FALSE, echo=FALSE, cache=FALSE}

knitr::opts_knit$set(root.dir=getwd())
knitr::opts_chunk$set(cache.lazy=FALSE,cache=TRUE,warning=FALSE,echo=TRUE,comment='',prompt=T)
knitr::opts_template$set(
  regular=list(include=TRUE,echo=TRUE,cache=FALSE),
  regular_cache=list(include=TRUE,echo=TRUE,cache=TRUE),
  invisible=list(include=FALSE,echo=FALSE,cache=FALSE,warning=FALSE,results=FALSE),
  muted=list(include=FALSE,echo=FALSE,cache=FALSE,warning=FALSE,results=FALSE),
  latex=list(include=TRUE,echo=TRUE,cache=FALSE))


data_regression<-as.data.frame(data_regression)
data_regression$n_hs_town_group<-data_regression$n_hs_town
data_regression$n_hs_town_group[data_regression$n_hs_town>=4 & data_regression$n_hs_town<=15]<-"4-15"
data_regression$n_hs_town_group[data_regression$n_hs_town>15]<-"16+"
data_regression$n_hs_town_group<-with(data_regression, reorder(n_hs_town_group, n_hs_town))

options(modelsummary_format_numeric_latex='plain')

variables<-c('fit_school_mean'='Instrumented Peer Admissiom Score (Percentile)',
             'fit_class_mean'='Instrumented Peer Admissiom Score (Percentile)')
             

```

## {.tabset}

### Stage 2
Run regression:

```{r, opts.label='regular'}
# data_regression<-data_regression %>%
#   mutate(instrument=as.factor(as.numeric(dec_town %in% c(1,2,3))*1000+as.numeric(n_hs_town_group=='3')))

data_regression2<-data_regression %>%
  mutate(d1=dec_town %in% c('1'),
         d2=dec_town %in% c('1','2'),
         d3=dec_town %in% c('1','2','3'),
         d4=dec_town %in% c('1','2','3','4'),
         d5=dec_town %in% c('1','2','3','4','5'),
         d6=dec_town %in% c('1','2','3','4','5','6'),
         d7=dec_town %in% c('1','2','3','4','5','6','7'),
         d8=dec_town %in% c('1','2','3','4','5','6','7','8'),
         d9=dec_town %in% c('1','2','3','4','5','6','7','8','9'),
         d10=dec_town %in% c('1','2','3','4','5','6','7','8','9','10'),
         n1=n_hs_town_group %in% c('1'),
         n2=n_hs_town_group %in% c('1','2'),
         n3=n_hs_town_group %in% c('1','2','3'),
         n15=n_hs_town_group %in% c('1','2','3','4-15'),
         n16=n_hs_town_group %in% c('1','2','3','4-15','16+'),
         n7=n_hs_town %in% 1:7)



model_school_full<-feols(grad_perc~entrance_perc+n_hs_town_group+dec_town*n_students_town_yr+Unemployment_hs_bac*dec_town+Wages_hs_bac*dec_town+drop_hs_hs_bac*dec_town|
        as.factor(an)+specializare_bac2+scoala_de_provenienta+town|
        school_mean~d5*n3,
      cluster=~judet_bac,
      data=data_regression2 %>% filter(school_change==F))
stage1_school_full<-summary(model_school_full,stage=1)


summary(model_school_full)
summary(model_school_full,stage=1)


model_class_full<-feols(grad_perc~entrance_perc+n_hs_town_group+dec_town*n_students_town_yr+Unemployment_hs_bac*dec_town+Wages_hs_bac*dec_town+drop_hs_hs_bac*dec_town|
        as.factor(an)+specializare_bac2+scoala_de_provenienta+town|
        class_mean~d5*n3,
      cluster=~judet_bac,
      data=data_regression2 %>% filter(school_change==F))
stage1_class_full<-summary(model_class_full,stage=1)

summary(model_class_full)
summary(model_class_full,stage=1)

```

```{r, opts.label='regular'}
f_big<-function(x) format(x, big.mark=",", scientific=FALSE, nsmall=1,digits=1)
f <- function(x) format(round(x/1000000,1) , nsmall = 1)
modelsummary(list("School"=model_school_full,
                  "Track"=model_class_full),
             statistic = "std.error",
             estimate="{estimate}{stars}",
            stars=c('$^{*}$'=0.1,'$^{**}$'=0.05,'$^{***}$'=0.01),
               gof_map=list(list("raw" = "nobs", "clean" = "N", "fmt" = f),
                          list("raw" = "r.squared", "clean" = "R$^2$",fmt="%.2f")),
              metrics="R2",
              #output="latex",
                escape=F)




```

```{r, opts.label='latex'}
f_big<-function(x) format(x, big.mark=",", scientific=FALSE, nsmall=1,digits=1)
f <- function(x) format(round(x/1000000,1) , nsmall = 1)
print(modelsummary(list("School"=model_school_full,
                  "Track"=model_class_full),
             statistic = "std.error",
             estimate="{estimate}{stars}",
            stars=c('$^{*}$'=0.1,'$^{**}$'=0.05,'$^{***}$'=0.01),
               gof_map=list(list("raw" = "nobs", "clean" = "N", "fmt" = f),
                          list("raw" = "r.squared", "clean" = "R$^2$",fmt="%.2f")),
              metrics="R2",
              output="latex",
                escape=F))
```